﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLChoDiaOc.Process;

namespace QLChoDiaOc
{
    /// <summary>
    /// Interaction logic for FormDKNguoiBan.xaml
    /// </summary>
    public partial class FormDKNguoiBan : Page
    {
        private InsertInfo _insertInfo = new InsertInfo();
        private SelectInfo _selectInfo = new SelectInfo();
        private DeleteInfo _deleteInfo = new DeleteInfo();
        private OtherHandle _otherHandle = new OtherHandle();
        private GetID _getId = new GetID();

        static bool m_bSelectData = false;

        public FormDKNguoiBan()
        {
            InitializeComponent();
            btnDongY.Visibility = Visibility.Collapsed;
            btnThem.Visibility = Visibility.Collapsed;
            maKHTextBox.Visibility = Visibility.Collapsed;
            KHCucomboBox.Visibility = Visibility.Collapsed;
            //btnMaKHAuto.Visibility = Visibility.Collapsed;
            maKH_temp_textbox.Visibility = Visibility.Collapsed;
            HienThiKhachHang();
        }

        void HienThiKhachHang()
        {
            if (App.IsDangKyKhachHang == true)
            {
                KHMoiradioButton.Visibility = Visibility.Collapsed;
                KHCuradioButton.Visibility = Visibility.Collapsed;
                maKH_temp_textbox.Visibility = Visibility.Visible;
                //maKH_temp_textbox.Text = App.Makhachhang.ToString();

                //KHACHHANG m_temp = new KHACHHANG();
                maKH_temp_textbox.Text = App.KhachHangTemp.MaKH;
                //m_temp = _selectInfo.SelectKhachHang(maKH_temp_textbox.Text);
                hoTenTextBox.Text = App.KhachHangTemp.HoTen; //m_temp.HoTen;
                ngaySinhDatePicker.SelectedDate = App.KhachHangTemp.NgaySinh; //m_temp.NgaySinh;
                diaChiTextBox.Text = App.KhachHangTemp.DiaChi; //m_temp.DiaChi;
                emailTextBox.Text = App.KhachHangTemp.Email; //m_temp.Email;
                soDTTextBox.Text = App.KhachHangTemp.SoDT; //m_temp.SoDT;
            }
        }

        void ResetAll()
        {
            maKHTextBox.Text = string.Empty;
            hoTenTextBox.Text = string.Empty;
            ngaySinhDatePicker.SelectedDate = null;
            diaChiTextBox.Text = string.Empty;
            emailTextBox.Text = string.Empty;
            soDTTextBox.Text = string.Empty;

            maKHTextBox.IsEnabled = false;
            hoTenTextBox.IsEnabled = false;
            ngaySinhDatePicker.IsEnabled = false;
            diaChiTextBox.IsEnabled = false;
            emailTextBox.IsEnabled = false;
            soDTTextBox.IsEnabled = false;
        }

        private void KHCucheckBox_Checked(object sender, RoutedEventArgs e)
        {
            //btnMaKHAuto.IsEnabled = false;
            KHCucomboBox.IsEnabled = true;
            for (int i = 0; i < _selectInfo.SelectAllMaKH().Count; i++)
                KHCucomboBox.Items.Add(_selectInfo.SelectAllMaKH()[i]);
        }

        private void KHCucomboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectData)
            {
                KHACHHANG m_temp = new KHACHHANG();
                m_temp = _selectInfo.SelectKhachHang(KHCucomboBox.SelectedItem.ToString());
                hoTenTextBox.Text = m_temp.HoTen;
                ngaySinhDatePicker.SelectedDate = m_temp.NgaySinh;
                diaChiTextBox.Text = m_temp.DiaChi;
                emailTextBox.Text = m_temp.Email;
                soDTTextBox.Text = m_temp.SoDT;
                m_bSelectData = true;
            }
            else
            {
                m_bSelectData = false;
            }
        }

        private void KHMoiradioButton_Checked(object sender, RoutedEventArgs e)
        {
            ResetAll();

            maKHTextBox.Visibility = Visibility.Visible;
            //btnMaKHAuto.Visibility = Visibility.Visible;
            KHCucomboBox.Visibility = Visibility.Collapsed;
            //btnMaKHAuto.IsEnabled = true;
            KHCucomboBox.SelectedItem = null;
            btnThem.Visibility = Visibility.Visible;
            btnDongY.Visibility = Visibility.Collapsed;

            //Ma KH moi
            maKHTextBox.Text = _getId.GenerateMaKH();
            hoTenTextBox.IsEnabled = true;
            ngaySinhDatePicker.IsEnabled = true;
            diaChiTextBox.IsEnabled = true;
            emailTextBox.IsEnabled = true;
            soDTTextBox.IsEnabled = true;
        }

        private void KHCuradioButton_Checked(object sender, RoutedEventArgs e)
        {
            ResetAll();

            maKHTextBox.Visibility = Visibility.Collapsed;
            KHCucomboBox.Visibility = Visibility.Visible;
            btnDongY.Visibility = Visibility.Visible;
            btnThem.Visibility = Visibility.Collapsed;
            //btnMaKHAuto.Visibility = Visibility.Collapsed;

            maKHTextBox.Text = "";
            hoTenTextBox.IsEnabled = false;
            ngaySinhDatePicker.IsEnabled = false;
            diaChiTextBox.IsEnabled = false;
            emailTextBox.IsEnabled = false;
            soDTTextBox.IsEnabled = false;

            KHCucomboBox.Items.Clear();
            for (int i = 0; i < _selectInfo.SelectAllMaKH().Count; i++)
                KHCucomboBox.Items.Add(_selectInfo.SelectAllMaKH()[i]);
        }

        private void btnDongY_Click(object sender, RoutedEventArgs e)
        {
            if (KHCucomboBox.SelectedItem == null)
                MessageBox.Show("Hãy chọn mã khách hàng", "Thông Báo");
            else
            {
                try
                {
                    App.KhachHangTemp.MaKH = KHCucomboBox.SelectedItem.ToString();
                    App.KhachHangTemp.HoTen = hoTenTextBox.Text;
                    App.KhachHangTemp.NgaySinh = DateTime.Parse(ngaySinhDatePicker.SelectedDate.ToString());
                    App.KhachHangTemp.DiaChi = diaChiTextBox.Text;
                    App.KhachHangTemp.Email = emailTextBox.Text;
                    App.KhachHangTemp.SoDT = soDTTextBox.Text;
                    MessageBox.Show("Thông tin hợp lệ", "Thông Báo");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Thông Báo");
                }

                App.Mainform.btnDangKyDiaOc.IsEnabled = true;
                //App.Makhachhang = KHCucomboBox.SelectedItem.ToString();
                btnDongY.IsEnabled = false;
                //btnHuy.IsEnabled = false;
                KHMoiradioButton.IsEnabled = false;
                KHCuradioButton.IsEnabled = false;
                KHCucomboBox.IsEnabled = false;
                App.IsDangKyKhachHang = true;

                App.IsKhachCu = true;
                App.IsKhachMoi = false;
            }
        }

        private void btnThem_Click(object sender, RoutedEventArgs e)
        {
            //if (maKHTextBox.Text == string.Empty)
            //    MessageBox.Show("Nhấn vào nút Cấp Mã khách hàng để nhận mã tự động", "Thông Báo");
            //else
            //{
            //int kt = _insertInfo.InsertKhachHang(maKHTextBox.Text,
            //                        hoTenTextBox.Text,
            //                        DateTime.Parse(ngaySinhDatePicker.SelectedDate.ToString()),
            //                        diaChiTextBox.Text,
            //                        emailTextBox.Text,
            //                        soDTTextBox.Text);


            //if (kt == 1)
            //    ResetAll();

            if (ngaySinhDatePicker.SelectedDate.Value.AddYears((int)_selectInfo.SelectThamSo("TuoiToiThieu")).Year <= DateTime.Now.Year)
            {
                try
                {
                    App.KhachHangTemp.MaKH = maKHTextBox.Text;
                    App.KhachHangTemp.HoTen = hoTenTextBox.Text;
                    App.KhachHangTemp.NgaySinh = DateTime.Parse(ngaySinhDatePicker.SelectedDate.ToString());
                    App.KhachHangTemp.DiaChi = diaChiTextBox.Text;
                    App.KhachHangTemp.Email = emailTextBox.Text;
                    App.KhachHangTemp.SoDT = soDTTextBox.Text;
                    MessageBox.Show("Thông tin hợp lệ", "Thông Báo");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Thông Báo");
                }

                btnThem.IsEnabled = false;
                //btnHuy.IsEnabled = false;
                KHMoiradioButton.IsEnabled = false;
                KHCuradioButton.IsEnabled = false;

                //btnMaKHAuto.IsEnabled = false;
                hoTenTextBox.IsEnabled = false;
                ngaySinhDatePicker.IsEnabled = false;
                diaChiTextBox.IsEnabled = false;
                emailTextBox.IsEnabled = false;
                soDTTextBox.IsEnabled = false;

                App.IsDangKyKhachHang = true;
                App.Mainform.btnDangKyDiaOc.IsEnabled = true;
                //App.Makhachhang = maKHTextBox.Text;
                App.IsKhachCu = false;
                App.IsKhachMoi = true;
            }
            else
            {
                MessageBox.Show("Kiểm tra lại ngày sinh, tuổi quá nhỏ", "Thông Báo");
            }
        }

        private void btnHuy_Click(object sender, RoutedEventArgs e)
        {
            ResetAll();
            btnDongY.Visibility = Visibility.Collapsed;
            btnThem.Visibility = Visibility.Collapsed;
            maKHTextBox.Visibility = Visibility.Collapsed;
            KHCucomboBox.Visibility = Visibility.Collapsed;
            //btnMaKHAuto.Visibility = Visibility.Collapsed;

            KHMoiradioButton.Visibility = Visibility.Visible;
            KHCuradioButton.Visibility = Visibility.Visible;
            maKH_temp_textbox.Visibility = Visibility.Collapsed;

            KHMoiradioButton.IsEnabled = true;
            KHCuradioButton.IsEnabled = true;

            App.Mainform.btnDangKyDiaOc.IsEnabled = false;
            App.Mainform.btnXacNhanGiayTo.IsEnabled = false;
            App.Mainform.btnDangKyDichVu.IsEnabled = false;
            App.Mainform.btnLapPhieuThu.IsEnabled = false;
            App.IsDangKyKhachHang = false;

            //Huy DiaOC
            App.IsDangKyDiaOc = false;

            //Huy QC
            App.IsDangKyQuangCao = false;
        }

        private void soDTTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _otherHandle.IsNumber(soDTTextBox);
        }

        //private void btnMaKHAuto_Click(object sender, RoutedEventArgs e)
        //{
        //    maKHTextBox.Text = _getId.GenerateMaKH();
        //    //btnMaKHAuto.IsEnabled = false;
        //    hoTenTextBox.IsEnabled = true;
        //    ngaySinhDatePicker.IsEnabled = true;
        //    diaChiTextBox.IsEnabled = true;
        //    emailTextBox.IsEnabled = true;
        //    soDTTextBox.IsEnabled = true;
        //}
    }
}
